<?php

namespace App\Admin\Controllers;

use App\Models\MpWeixinCompany;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\Box;
use Illuminate\Support\Str;
use App\Admin\Actions\BlindBoxAction\RefreshBlindBoxRate;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Support\Facades\Storage;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Admin;

class WxCompanyController extends AdminController
{
    protected $title = '微信公众号';
    

    protected function grid()
    {
        $grid = new Grid(new MpWeixinCompany());
	
	    $grid->disableExport();

	     $grid->column('images', '公众号图标')->display(function ($value) {
	        if (! Storage::exists($value)) {
		       // $this->update(['cover' => null]);
		        return '';
	        } else {
		        return _grid_image($value);
	        }
        });
	    
	    $grid->column('nickname', '公众号名称');

	    $grid->column('fakeid', '公众号ID');
	   
	    $grid->column('created_at', '创建时间')->display(function () {
	    	return $this->created_at->toDatetimeString();
	    });
	   	$this->gridActions($grid);
        return $grid;
    }
    

   	protected function gridActions(Grid $grid)
	{
		$grid->actions(function (Grid\Displayers\Actions $actions) {
			$actions->disableView();
			//$actions->add(new RefreshBlindBoxRate());
		});
	}

    protected function detail($id)
    {
        $show = new Show(MpWeixinCompany::findOrFail($id));
        return $show;
    }
    
    protected function form()
    {
        $form = new Form(new MpWeixinCompany());

	    $form->select('fakeid', '公众号ID')->ajax('/admin/api/wxCompany');
	    $form->hidden('nickname', '公众号名称');
	   
	     Admin::script(<<<JS
		    $(document).on('change', '.fakeid', function() {
		    	console.log(123);
		        const nickname = $(this).find(':selected').html();
		        console.log(123);
		        $('.nickname').val(nickname);
		    });
		JS);
        return $form;
    }

   
}
